<template>
  <a-modal
    title="新增物料信息"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <a-row>
          <a-col :span="12">
            <a-form-item label="物料编号" has-feedback>
              <a-input
                placeholder="请输入物料编号"
                v-decorator="['materialNo', { rules: [{ required: true, message: '请输入物料编号！' }] }]"
              />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="物料名称" has-feedback>
              <a-input
                placeholder="请输入物料名称"
                v-decorator="['materialName', { rules: [{ required: true, message: '请输入物料名称！' }] }]"
              />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="物料类别">
              <a-select
                style="width: 100%"
                placeholder="请选择物料类别"
                v-decorator="['materialType', { rules: [{ required: true, message: '请选择物料类别！' }] }]"
              >
                <a-select-option v-for="(item, index) in materialTypeData" :key="index" :value="item.code">{{
                  item.name
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="物料尺寸" has-feedback>
              <a-input placeholder="请输入物料尺寸" v-decorator="['materialSpec']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="物料密度" has-feedback>
              <a-input placeholder="请输入物料密度" v-decorator="['materialDensity']" />
            </a-form-item>
          </a-col>

          <!-- <a-form-item label="库区参数"  has-feedback>
          <a-select mode="multiple" style="width: 100%" placeholder="请选择库区参数" v-decorator="['areas']">
            <a-select-option v-for="(item,index) in areaNameParameterData" :key="index" :value="item.areaId">{{ item.areaName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="工作区参数"  has-feedback>
          <a-select mode="multiple" style="width: 100%" placeholder="请选择工作区参数" v-decorator="['stations']">
            <a-select-option v-for="(item,index) in stationNameParameterData" :key="index" :value="item.stationId">{{ item.stationName }}</a-select-option>
          </a-select>
        </a-form-item> -->

          <a-col :span="12">
            <a-form-item label="物料批次" has-feedback>
              <a-input
                placeholder="请输入物料批次"
                v-decorator="['materialBatch', { rules: [{ required: true, message: '请输入物料批次！' }] }]"
              />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="物料检验" has-feedback>
              <a-select
                style="width: 100%"
                placeholder="请选择物料检验"
                v-decorator="['inspectionMethod', { rules: [{ required: true, message: '请选择物料检验！' }] }]"
              >
                <a-select-option v-for="(item, index) in inspectionMethodData" :key="index" :value="item.code">{{
                  item.name
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="物料单位" has-feedback>
              <a-input placeholder="请输入物料单位" v-decorator="['materialUnit']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="安全存量" has-feedback>
              <a-input type="number" min="1" placeholder="请输入安全存量" v-decorator="['safeqty']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="最大存量" has-feedback>
              <a-input type="number" min="1" placeholder="请输入最大存量" v-decorator="['maxImumqty']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="最小库龄" has-feedback>
              <a-input type="number" min="1" placeholder="请输入最小库龄" v-decorator="['minstorageAge']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="最大库龄" has-feedback>
              <a-input type="number" min="1" placeholder="请输入最大库龄" v-decorator="['maxstorageAge']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="描述" has-feedback>
              <a-textarea :rows="4" placeholder="请输入描述" v-decorator="['description']"></a-textarea>
            </a-form-item>
          </a-col>

          <!-- <a-form-item label="库区Ids"  has-feedback>
          <a-input placeholder="请输入库区Ids" v-decorator="['areaIds']" />
        </a-form-item> -->
          <!-- <a-form-item label="库区参数"  has-feedback>
          <a-input placeholder="请输入库区参数" v-decorator="['areaNameParameter']" />
        </a-form-item> -->
          <!-- <a-form-item label="工作区Ids"  has-feedback>
          <a-input placeholder="请输入工作区Ids" v-decorator="['stationIds']" />
        </a-form-item> -->
          <!-- <a-form-item label="工作区参数"  has-feedback>
          <a-input placeholder="请输入工作区参数" v-decorator="['stationNameParameter']" />
        </a-form-item> -->
          <!-- <a-form-item label="工段"  has-feedback>
          <a-input placeholder="请输入工段" v-decorator="['lesWorkshopSection']" />
        </a-form-item> -->

          <!-- <a-form-item label="签核状态"  has-feedback>
          <a-input placeholder="请输入签核状态" v-decorator="['issueState']" />
        </a-form-item> -->

          <!-- <a-form-item label="熟化时间"  has-feedback>
          <a-input-number placeholder="请输入熟化时间" style="width: 100%" v-decorator="['maturationTime']" />
        </a-form-item> -->
          <!-- <a-form-item label="是否需要熟化" >
          <a-switch v-decorator="['isMaturation', { valuePropName: 'checked' }]" />
        </a-form-item> -->
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { WmsMaterialAdd, GetAreas, GetStations } from '@/api/modular/main/WmsMaterialManage'
export default {
  data() {
    return {
      labelCol: {
        // xs: { span: 24 },
        // sm: { span: 5 }
        span: 8
      },
      wrapperCol: {
        // xs: { span: 24 },
        // sm: { span: 15 }
        span: 13
      },
      inspectionMethodData: [],
      materialTypeData: [],
      areaNameParameterData: [],
      stationNameParameterData: [],
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    // 初始化方法
    add(record) {
      this.visible = true
      this.$nextTick(() => {
        this.getSelects()
      })
      const materialTypeOption = this.$options
      this.materialTypeData = materialTypeOption.filters['dictData']('material_type')
      const inspectionMethodOption = this.$options
      this.inspectionMethodData = inspectionMethodOption.filters['dictData']('material_inspection')
    },
    getSelects() {
      if (this.areaNameParameterData.length <= 0 || this.stationNameParameterData.length <= 0) {
        this.confirmLoading = true
        Promise.all([GetAreas(), GetStations()])
          .then(response => {
            this.areaNameParameterData = response[0].data || []
            this.stationNameParameterData = response[1].data || []
            this.confirmLoading = false
          })
          .catch(() => {
            this.confirmLoading = false
          })
      }
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      const {
        form: { validateFields }
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          if (this.$getObjectType(values.areas) === 'array') {
            values.areaIds = values.areas.join(',')
            values.areaNameParameterArr = values.areas.map(val => {
              return this.parseSelectName(val, 'areaId', 'areaName', 'areaNameParameterData')
            })
            values.areaNameParameter = values.areaNameParameterArr.join(',')
          }
          if (this.$getObjectType(values.stations) === 'array') {
            values.stationIds = values.stations.join(',')
            values.stationNameParameterArr = values.stations.map(val => {
              return this.parseSelectName(val, 'stationId', 'stationName', 'stationNameParameterData')
            })
            values.stationNameParameter = values.stationNameParameterArr.join(',')
          }
          WmsMaterialAdd(values)
            .then(res => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败：' + JSON.stringify(res.message))
              }
            })
            .finally(res => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    parseSelectName(val, field, nfield, pfield) {
      let res = val
      for (let i = 0; i < this[pfield].length; i++) {
        if (val === this[pfield][i][field]) {
          res = this[pfield][i][nfield]
          break
        }
      }
      return res
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
